<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base;

interface ISession
{
  /**
   * Inits session
   *
   * @static
   */
  public static function init();

  /**
   * Close session
   *
   * @static
   */
  public static function close();

  /**
   * Does specified key exists in session?
   * Respects application identifiers
   *
   * @static
   * @param $sKey string  Key to check
   * @return  bool
   */
  public static function exists($sKey);
  public static function existsNamespace($sNamespace, $sKey);

  public static function set($sKey, $value);
  public static function setNamespace($sNamespace, $skey, $value);

  public static function get($sKey);
  public static function getNamespace($sNamespace, $sKey);

  public static function getDefault($sKey, $default);
  public static function getNamespaceDefault($sNamespace, $sKey, $default);

  public static function remove($sKey);
  public static function removeNamespace($sNamespace, $sKey);
}

class Session implements ISession
{
  public static function init()
  {
    $id = \session_id();
    if ($id == '')
    {
      @\session_start();
    }
  }

  public static function close()
  {
    \session_write_close();
  }

  public static function exists($sKey)
  {
    if (isset($_SESSION[Application::getVendor()][Application::getProject()][$sKey]))
    {
      return \TRUE;
    }
    else
    {
      return \FALSE;
    }
  }

  public static function existsNamespace($sNamespace, $sKey)
  {
    if (isset($_SESSION[Application::getVendor()][Application::getProject()][$sNamespace][$sKey]))
    {
      return \TRUE;
    }
    else
    {
      return \FALSE;
    }
  }

  public static function set($sKey, $value)
  {
    $_SESSION[Application::getVendor()][Application::getProject()][$sKey] = $value;
  }

  public static function setNamespace($sNamespace, $sKey, $value)
  {
    $_SESSION[Application::getVendor()][Application::getProject()][$sNamespace][$sKey] = $value;
  }

  public static function get($sKey)
  {
    if (self::exists($sKey))
    {
      return $_SESSION[Application::getVendor()][Application::getProject()][$sKey];
    }
    else
    {
      throw new Exception(session\Exception::ERROR_KEYNOTINSESSION,
                          array('key' => $sKey));
    }
  }

  public static function getNamespace($sNamespace, $sKey)
  {
    if (self::existsNamespace($sNamespace, $sKey))
    {
      return $_SESSION[Application::getVendor()][Application::getProject()][$sNamespace][$sKey];
    }
    else
    {
      throw new Exception(session\Exception::ERROR_KEYNOTINSESSION,
        array('key' => $sKey));
    }
  }

  public static function getDefault($sKey, $default)
  {
    try
    {
      return self::get($sKey);
    }
    catch (Exception $e)
    {
      if ($e->getMessage() == session\Exception::ERROR_KEYNOTINSESSION)
      {
        return $default;
      }
      throw $e;
    }
  }

  public static function getNamespaceDefault($sNamespace, $sKey, $default)
  {
    try
    {
      return self::getNamespace($sNamespace, $sKey);
    }
    catch (Exception $e)
    {
      if ($e->getMessage() == session\Exception::ERROR_KEYNOTINSESSION)
      {
        return $default;
      }
      throw $e;
    }
  }

  public static function remove($sKey)
  {
    if (self::exists($sKey))
    {
      unset($_SESSION[Application::getVendor()][Application::getProject()][$sKey]);
    }
  }

  public static function removeNamespace($sNamespace, $sKey)
  {
    if (self::existsNamespace($sNamespace, $sKey))
    {
      unset($_SESSION[Application::getVendor()][Application::getProject()][$sNamespace][$sKey]);
    }
  }
}